{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%%shell\n# Installs the latest dev build of TVM from PyPI. If you wish to build\n# from source, see https://tvm.apache.org/docs/install/from_source.html\npip install apache-tvm --pre"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# 2. microTVM TFLite Tutorial\n**Author**: [Tom Gall](https://github.com/tom-gall)\n\nThis tutorial is an introduction to working with microTVM and a TFLite\nmodel with Relay.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Install microTVM Python dependencies\n\nTVM does not include a package for Python serial communication, so\nwe must install one before using microTVM. We will also need TFLite\nto load models.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%%shell\npip install pyserial==3.5 tflite==2.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\n# By default, this tutorial runs on x86 CPU using TVM's C runtime. If you would like\n# to run on real Zephyr hardware, you must export the `TVM_MICRO_USE_HW` environment\n# variable. Otherwise (if you are using the C runtime), you can skip installing\n# Zephyr. It takes ~20 minutes to install Zephyr.\nuse_physical_hw = bool(os.getenv(\"TVM_MICRO_USE_HW\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Install Zephyr\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%%shell\n# Install west and ninja\npython3 -m pip install west\napt-get install -y ninja-build\n\n# Install ZephyrProject\nZEPHYR_PROJECT_PATH=\"/content/zephyrproject\"\nexport ZEPHYR_BASE=${ZEPHYR_PROJECT_PATH}/zephyr\nwest init ${ZEPHYR_PROJECT_PATH}\ncd ${ZEPHYR_BASE}\ngit checkout v3.2-branch\ncd ..\nwest update\nwest zephyr-export\nchmod -R o+w ${ZEPHYR_PROJECT_PATH}\n\n# Install Zephyr SDK\ncd /content\nZEPHYR_SDK_VERSION=\"0.15.2\"\nwget \"https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${ZEPHYR_SDK_VERSION}/zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz\"\ntar xvf \"zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz\"\nmv \"zephyr-sdk-${ZEPHYR_SDK_VERSION}\" zephyr-sdk\nrm \"zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz\"\n\n# Install python dependencies\npython3 -m pip install -r \"${ZEPHYR_BASE}/scripts/requirements.txt\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Import Python dependencies\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import json\nimport tarfile\nimport pathlib\nimport tempfile\nimport numpy as np\n\nimport tvm\nimport tvm.micro\nimport tvm.micro.testing\nfrom tvm import relay\nimport tvm.contrib.utils\nfrom tvm.micro import export_model_library_format\nfrom tvm.contrib.download import download_testdata\n\nmodel_url = (\n    \"https://github.com/tlc-pack/web-data/raw/main/testdata/microTVM/model/sine_model.tflite\"\n)\nmodel_file = \"sine_model.tflite\"\nmodel_path = download_testdata(model_url, model_file, module=\"data\")\n\ntflite_model_buf = open(model_path, \"rb\").read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Using the buffer, transform into a tflite model python object\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    import tflite\n\n    tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)\nexcept AttributeError:\n    import tflite.Model\n\n    tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Print out the version of the model\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "version = tflite_model.Version()\nprint(\"Model Version: \" + str(version))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Parse the python model object to convert it into a relay module\nand weights.\nIt is important to note that the input tensor name must match what\nis contained in the model.\n\nIf you are unsure what that might be, this can be discovered by using\nthe ``visualize.py`` script within the Tensorflow project.\nSee [How do I inspect a .tflite file?](https://www.tensorflow.org/lite/guide/faq)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "input_tensor = \"dense_4_input\"\ninput_shape = (1,)\ninput_dtype = \"float32\"\n\nmod, params = relay.frontend.from_tflite(\n    tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype}\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Defining the target\n\nNow we create a build config for relay, turning off two options and then calling relay.build which\nwill result in a C source file for the selected TARGET. When running on a simulated target of the\nsame architecture as the host (where this Python script is executed) choose \"crt\" below for the\nTARGET, the C Runtime as the RUNTIME and a proper board/VM to run it (Zephyr will create the right\nQEMU VM based on BOARD. In the example below the x86 arch is selected and a x86 VM is picked up accordingly:\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "RUNTIME = tvm.relay.backend.Runtime(\"crt\", {\"system-lib\": True})\nTARGET = tvm.micro.testing.get_target(\"crt\")\n\n# When running on physical hardware, choose a TARGET and a BOARD that describe the hardware. The\n# STM32L4R5ZI Nucleo target and board is chosen in the example below. You could change the testing\n# board by simply exporting `TVM_MICRO_BOARD` variable with a different Zephyr supported board.\n\nif use_physical_hw:\n    BOARD = os.getenv(\"TVM_MICRO_BOARD\", default=\"nucleo_l4r5zi\")\n    SERIAL = os.getenv(\"TVM_MICRO_SERIAL\", default=None)\n    TARGET = tvm.micro.testing.get_target(\"zephyr\", BOARD)\n\n# For some boards, Zephyr runs them emulated by default, using QEMU. For example, below is the\n# TARGET and BOARD used to build a microTVM firmware for the mps2-an521 board.\n#\n# `mps2_an521 = \"mps2_an521\"`\n# `TARGET = tvm.micro.testing.get_target(\"zephyr\", BOARD)`"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, compile the model for the target. If you do not specify Executor,\nby default it uses GraphExecutor.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with tvm.transform.PassContext(opt_level=3, config={\"tir.disable_vectorize\": True}):\n    module = relay.build(mod, target=TARGET, runtime=RUNTIME, params=params)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Inspecting the compilation output\n\nThe compilation process has produced some C code implementing the operators in this graph. We\ncan inspect it by printing the CSourceModule contents (for the purposes of this tutorial, let's\njust print the first 10 lines):\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "c_source_module = module.get_lib().imported_modules[0]\nassert c_source_module.type_key == \"c\", \"tutorial is broken\"\n\nc_source_code = c_source_module.get_source()\nfirst_few_lines = c_source_code.split(\"\\n\")[:10]\nassert any(\n    l.startswith(\"TVM_DLL int32_t tvmgen_default_\") for l in first_few_lines\n), f\"tutorial is broken: {first_few_lines!r}\"\nprint(\"\\n\".join(first_few_lines))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Compiling the generated code\n\nNow we need to incorporate the generated C code into a project that allows us to run inference on the\ndevice. The simplest way to do this is to integrate it yourself, using microTVM's standard output format\nmodel library format. This is a tarball with a standard layout.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get a temporary path where we can store the tarball (since this is running as a tutorial).\n\ntemp_dir = tvm.contrib.utils.tempdir()\nmodel_tar_path = temp_dir / \"model.tar\"\nexport_model_library_format(module, model_tar_path)\n\nwith tarfile.open(model_tar_path, \"r:*\") as tar_f:\n    print(\"\\n\".join(f\" - {m.name}\" for m in tar_f.getmembers()))\n\n# TVM also provides a standard way for embedded platforms to automatically generate a standalone\n# project, compile and flash it to a target, and communicate with it using the standard TVM RPC\n# protocol. The Model Library Format serves as the model input to this process. When embedded\n# platforms provide such an integration, they can be used directly by TVM for both host-driven\n# inference and autotuning . This integration is provided by the\n# `microTVM Project API` <https://github.com/apache/tvm-rfcs/blob/main/rfcs/0008-microtvm-project-api.md>_,\n#\n# Embedded platforms need to provide a Template Project containing a microTVM API Server (typically,\n# this lives in a file ``microtvm_api_server.py`` in the root directory). Let's use the example ``host``\n# project in this tutorial, which simulates the device using a POSIX subprocess and pipes:\n\ntemplate_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects(\"crt\"))\nproject_options = {}  # You can use options to provide platform-specific options through TVM.\n\n#  For physical hardware, you can try out the Zephyr platform by using a different template project\n#  and options:\n\nif use_physical_hw:\n    template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects(\"zephyr\"))\n    project_options = {\n        \"project_type\": \"host_driven\",\n        \"board\": BOARD,\n        \"serial_number\": SERIAL,\n        \"config_main_stack_size\": 4096,\n        \"zephyr_base\": os.getenv(\"ZEPHYR_BASE\", default=\"/content/zephyrproject/zephyr\"),\n    }\n\n# Create a temporary directory\ntemp_dir = tvm.contrib.utils.tempdir()\ngenerated_project_dir = temp_dir / \"generated-project\"\ngenerated_project = tvm.micro.generate_project(\n    template_project_path, module, generated_project_dir, project_options\n)\n\n# Build and flash the project\ngenerated_project.build()\ngenerated_project.flash()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, establish a session with the simulated device and run the\ncomputation. The `with session` line would typically flash an attached\nmicrocontroller, but in this tutorial, it simply launches a subprocess\nto stand in for an attached microcontroller.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with tvm.micro.Session(transport_context_manager=generated_project.transport()) as session:\n    graph_mod = tvm.micro.create_local_graph_executor(\n        module.get_graph_json(), session.get_system_lib(), session.device\n    )\n\n    # Set the model parameters using the lowered parameters produced by `relay.build`.\n    graph_mod.set_input(**module.get_params())\n\n    # The model consumes a single float32 value and returns a predicted sine value.  To pass the\n    # input value we construct a tvm.nd.array object with a single contrived number as input. For\n    # this model values of 0 to 2Pi are acceptable.\n    graph_mod.set_input(input_tensor, tvm.nd.array(np.array([0.5], dtype=\"float32\")))\n    graph_mod.run()\n\n    tvm_output = graph_mod.get_output(0).numpy()\n    print(\"result is: \" + str(tvm_output))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.16"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}